﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace WEI_surrogate
{
    static class Program
    {
        static string[] Captions = { "Base score:", String.Empty, "Processor:", "Memory (RAM):", "Graphics:", "Gaming graphics:", "Primary hard disk:" };
        static int MaxLen;
        static string[] Tags = { "SystemScore", String.Empty, "CpuScore", "MemoryScore", "GraphicsScore", "GamingScore", "DiskScore" };
        static XmlDocument RawResults;

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var TempFile = Path.GetTempFileName();
            TempFile = Path.ChangeExtension(TempFile, ".xml");
            var Benchmark = new Process();
            Benchmark.StartInfo.FileName = "WinSAT";
            Benchmark.StartInfo.Arguments = "formal -restart clean -xml " + TempFile;
            Benchmark.Start();
            Benchmark.WaitForExit();
            RawResults = new XmlDocument();
            RawResults.LoadXml(File.ReadAllText(TempFile));
            File.Delete(TempFile);
            var Results = new StringBuilder();
            foreach (string Caption in Captions)
                if (Caption.Length > MaxLen)
                    MaxLen = Caption.Length;
            for (int i = 0; Captions.Length > i; i++)
                Results.AppendLine(MakeLine(i));
            MessageBox.Show(Results.ToString(), GetAppName());
        }

        private static string GetAppName()
        {
            try
            {
                throw (new Exception());
            }
            catch (Exception ex)
            {
                return ex.Source;
            }
        }

        private static string MakeLine(int Index)
        {
            return Captions[Index].PadRight(MaxLen) + "\t" + RawResults.GetTagText(Tags[Index]);
        }

        private static string GetTagText(this XmlDocument XML, string Tag)
        {
            return XML.GetElementsByTagName(Tag)[0]?.InnerText ?? String.Empty;
        }

    }
}